import jdk.nashorn.internal.parser.JSONParser;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet("/wfs")
public class wfsImplication extends HttpServlet {
    private static Connection conn;

    public wfsImplication() throws Exception{
        Class.forName("org.postgresql.Driver");
        String url = "jdbc:postgresql://localhost:5432/Vancouver";
        conn = DriverManager.getConnection(url, "postgres", "123");
    }

    public void destroy() {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    // geojson 格式，构造返回
//    {
//        "type": "FeatureCollection",
//        "features": [
//            {"type":"Feature",
//                  "properties":{},
//                "geometry":{
//                "type":"Point",
//                        "coordinates":[105.380859375,31.57853542647338]
//            }
//            }
//        ]
//    }

    // 内部类 根据图层属性多少进行修改，get,set可以自动生成
    public class Properties{
        private int id;
        private int num;

        // 一定要有空的构造函数
        public Properties(){}
        public Properties(int id, int num) {
            this.id = id;
            this.num = num;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        @Override
        public String toString() {
            return "Properties{" +
                    "id=" + id +
                    ", num=" + num +
                    '}';
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT *, st_asgeojson(geometry) as feature FROM prj_weilan where 1=1");

            JSONArray features = new JSONArray();
            // 循环查询的featur记录
            while ( rs.next() ) {
                // 取属性
                int id = rs.getInt("id");
                int num = rs.getInt("num");
                // 构造javabean 对象
                Properties properties = new Properties();
                properties.setId(id);
                properties.setNum(num);

                String geometry = rs.getString("feature");
                JSONObject feature = new JSONObject();
                feature.put("type","Feature");
                feature.put("geometry",geometry);
                feature.put("properties",JSONObject.fromObject(properties));
                features.put(feature);

                System.out.println( "properties = " + properties.toString() );
                System.out.println( "geometry = " + geometry );
                System.out.println();
            }
            JSONObject result = new JSONObject();
            result.put("type","FeatureCollection");
            result.put("features",features);

            resp.getWriter().write(result.toString());
        }catch (Exception exception){
            exception.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}
